package com.yuanluludev.lib_logcat.crash;

import android.app.Activity;
import android.app.Application;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.yuanluludev.lib_logcat.logcat.R;
import com.yuanluludev.lib_logcat.utils.MetaDataUtil;


/**
 * desc   : 崩溃捕捉界面
 */
public final class CrashActivity extends Activity implements View.OnClickListener {
    private AlertDialog mDialog;

    public static void start(Application application, String throwable) {
        if (throwable == null) {
            return;
        }
        Intent intent = new Intent(application, CrashActivity.class);
        intent.putExtra("other", throwable);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        application.startActivity(intent);
    }

    private DrawerLayout mDrawerLayout;
    private TextView mInfoView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.crash_activity);
        initView();

    }

    protected void initView() {
        mDrawerLayout = findViewById(R.id.dl_crash_drawer);
        mInfoView = findViewById(R.id.tv_crash_info);
        findViewById(R.id.iv_crash_info).setOnClickListener(this);
        findViewById(R.id.iv_crash_share).setOnClickListener(this);
        findViewById(R.id.iv_crash_restart).setOnClickListener(this);
        findViewById(R.id.btn_crash_log).setOnClickListener(this);
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            mInfoView.setText(getIntent().getStringExtra("other"));
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_crash_info) {
            mDrawerLayout.openDrawer(GravityCompat.START);
        } else if (id == R.id.iv_crash_share) {// 分享文本
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, mInfoView.getText());
            startActivity(Intent.createChooser(intent, ""));
        } else if (id == R.id.iv_crash_restart) {// 重启应用
            try {
                String http = MetaDataUtil.Companion.getString("restart_app",getApplicationContext());
                Intent intent = new Intent(CrashActivity.this, Class.forName(http));
                startActivity(intent);
            } catch (Exception e) {
            }
        } else if (id == R.id.btn_crash_log) {
            if (mDialog == null) {
                mDialog = new AlertDialog.Builder(CrashActivity.this)
                        .setTitle("错误详情")
                        .setMessage(mInfoView.getText())
                        .setPositiveButton("关闭", null)
                        .setNeutralButton("复制日志", (dialog, which) -> copyErrorToClipboard(mInfoView.getText().toString()))
                        .create();
            }
            mDialog.show();
            TextView textView = mDialog.findViewById(android.R.id.message);
            if (textView != null) {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            }
        }
    }

    /**
     * 复制报错信息到剪贴板
     */
    private void copyErrorToClipboard(String text) {
        ClipboardManager systemService = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        systemService.setPrimaryClip(ClipData.newPlainText("text", text));
    }

    @Override
    public void onBackPressed() {
        onClick(findViewById(R.id.iv_crash_restart));
        finish();
    }

}